# $Id: dcshared.tcl 32 2007-08-25 22:17:45Z khomoutov $
# Standalone "daemon" implementing "DC share" functionality.
# In essense, it's a networked interface to the "dcshare" package.
# The idea of this code is taken from tclxmppd by Patrick Thoyts:
# http://tclxmppd.googlecode.com/svn/trunk/demos/chime.tcl

lappend auto_path ../tcldc ;# TODO remove in production

package require dcshare

namespace eval ::dcshared {
}

proc ::dcshared::Main {} {
	global tcl_platform tcl_interactive tcl_service tk_version
	variable Options
#	LoadConfig

	# Setup control stream.
	if {$tcl_platform(platform) eq "unix"} {
		set cmdloop [file join [file dirname [info script]] cmdloop.tcl]
		if {[file exists $cmdloop]} {
			puts "Loading $cmdloop"            
			source $cmdloop
			set cmdloop::welcome "$Options(Name) v[set [namespace current]::version]"
			append cmdloop::welcome "\nReady for input from %client %port"
			cmdloop::cmdloop
			#set cmdloop::hosts_allow {127.0.0.1 ::1}
			cmdloop::listen 127.0.0.1 5442;# could do 0.0.0.0 5441
		} else {
			puts "Command loop not available."
		}
		set tcl_interactive 1; # fake it so we can re-source this file
	}

	# Begin the component
#	start

	# Loop forever, dealing with Wish or Tclsh
	if {[info exists tk_version]} {
		if {[tk windowingsystem] eq "win32"} { console show }
		wm withdraw .
		tkwait variable ::forever
#		stop
	} else {
		# Permit running as a Windows service.
		if {![info exists tcl_service]} {
			vwait ::forever
#			stop
		}
	}
}

if {!$tcl_interactive} {
	set r [catch [linsert $argv 0 ::dcshared::Main] err]
	if {$r} {puts $errorInfo}
	exit $r
}

